﻿using CommunicationProtocol.dto;
using CommunicationProtocol.dto.command;
using server.filters;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Forms;
using static client.utils.ImageUtils;
using static client.utils.Constant;
namespace client.render
{
    internal class ObjectCreator //: CommandExecutor
    {
        private Form form;


        protected int xLocationOffset { get; set; }
        protected int yLocationOffset { get; set; }

        private readonly int defaultWidth = singlePlantWidth;
        private readonly int defaultHeight = singlePlantHeight;

        public ObjectCreator(Form form, int xLocationOffset, int yLocationOffset)
        {
            this.form = form;
            this.xLocationOffset = xLocationOffset;
            this.yLocationOffset = yLocationOffset;

        }



        public void Execute(BaseObject baseObj)
        {
          /*  if (!(buffer is BaseObject))
            {
                throw new ArgumentException("数据类型异常" + buffer.GetType());
            }

            BaseObject baseObj = (BaseObject)buffer;
*/

            List<Image> imageList = loadImagesByObjectType(baseObj.ObjectType);

            PictureBox pictureBox = new PictureBox();
            pictureBox.Name = baseObj.ObjectId.ToString();

            pictureBox.Visible = true;
            pictureBox.Image = imageList[0];
            pictureBox.Size = new Size(defaultWidth, defaultHeight);
            pictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
            //pictureBox.trans
            pictureBox.Location = new Point(
                xLocationOffset + baseObj.LocationX * defaultWidth,
                yLocationOffset + baseObj.LocationY * defaultHeight
                );

            

            beforeRender(imageList, pictureBox);
            try
            {
                /*
                 * form.Controls.Add(pictureBox);
                 +		ex	{"线程间操作无效: 从不是创建控件“MainGameForm”的线程访问它。"}	
                System.Exception {System.InvalidOperationException}

                 */
                // 在UI线程上执行将按钮添加到窗体的操作
                form.Invoke((MethodInvoker)delegate
                {
                    form.Controls.Add(pictureBox);

                    // 因为这个方法 BringToFront() 涉及与 parent 的相对位置
                    // 所以只有等 picturebox 渲染之后再调用才能生效
                    pictureBox.BringToFront();
                    pictureBox.BackColor = Color.Transparent;

                    afterRender(pictureBox, baseObj);
                });

                Console.WriteLine("成功创建pictureBox对象");

            }
            catch (Exception ex)
            {

                Console.WriteLine("创建控件失败：" + ex.ToString());
            }
        }

        protected virtual void beforeRender(List<Image> imageList, PictureBox pictureBox)
        {
           
        }

        protected virtual void afterRender( PictureBox pictureBox, BaseObject baseObj)
        {

        }
    }
}
